package com.panda.business.service;

import com.panda.business.domain.CommontagInfo;
import com.panda.business.domain.req.CommontagVo;
import com.panda.business.mapper.CommontagMapper;
import com.panda.common.utils.DateUtils;
import com.panda.framework.domain.base.DBPage;
import com.panda.framework.domain.base.PageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional // 支持事务
public class CommontagService {

	@Autowired
	private CommontagMapper commontagMapper;

	public PageBean<CommontagInfo> queryCommontagInfo(CommontagVo info) {
		 
		DBPage page = new DBPage(info.getPageSize(), (info.getPageNo() - 1) * info.getPageSize());
		
		List<CommontagInfo> resultList = commontagMapper.queryCommontagInfo(page,info.getName(),info.getCid(),info.getDid());
		Integer totalResult = commontagMapper.queryCommontagInfoCount(page,info.getName(),info.getCid(),info.getDid());
		int pageTotal = totalResult % info.getPageSize() == 0 ? totalResult / info.getPageSize(): totalResult / info.getPageSize() + 1;

		PageBean<CommontagInfo> pageBean = new PageBean<CommontagInfo>(info.getPageSize(), info.getPageNo(), pageTotal, resultList);

		return pageBean;
	}

 
	public List<CommontagInfo> getAllCommontagInfoByCid(Integer cid){
		List<CommontagInfo> list = commontagMapper.getAllCommontagInfoByCid(cid);
		return list;
	}
	
	public List<CommontagInfo> getAllCommontagInfoByCidDid(Integer cid,Long did){
		List<CommontagInfo> list = commontagMapper.getAllCommontagInfoByCidDid(1,did);
		return list;
	}
	
	public CommontagInfo getCommontagInfoByid(Integer id){
		CommontagInfo user = commontagMapper.findCommontagInfoByid(id);
		return user;
	}
	
	public String insert(CommontagInfo info) {
		String res = "success";
		try {
			commontagMapper.insert(info);
		} catch (Exception e) {
			res = "fail";
			e.printStackTrace();
		}
		return res;
	}

	public String update(CommontagInfo info) {
		String res = "success";
		try {
			commontagMapper.update(info);
		} catch (Exception e) {
			res = "fail";
			e.printStackTrace();
		}
		return res;
	}

	public void delete(Integer id) {
		CommontagInfo info = new CommontagInfo();
		info.setId(id);
		commontagMapper.delete(info);
	}

	/**
	 * 查询部门公共标签
	 *
	 * @param id 部门公共标签ID
	 * @return 部门公共标签
	 */
	public CommontagInfo selectTblCommontaginfoById(Long id)
	{
		return commontagMapper.selectTblCommontaginfoById(id);
	}

	/**
	 * 查询部门公共标签列表
	 *
	 * @param tblCommontaginfo 部门公共标签
	 * @return 部门公共标签
	 */
	
	public List<CommontagInfo> selectTblCommontaginfoList(CommontagInfo tblCommontaginfo)
	{
		return commontagMapper.selectTblCommontaginfoList(tblCommontaginfo);
	}

	/**
	 * 新增部门公共标签
	 *
	 * @param tblCommontaginfo 部门公共标签
	 * @return 结果
	 */
	
	public int insertTblCommontaginfo(CommontagInfo tblCommontaginfo)
	{
		tblCommontaginfo.setCreateTime(DateUtils.getNowDate());
		return commontagMapper.insertTblCommontaginfo(tblCommontaginfo);
	}

	/**
	 * 修改部门公共标签
	 *
	 * @param tblCommontaginfo 部门公共标签
	 * @return 结果
	 */
	
	public int updateTblCommontaginfo(CommontagInfo tblCommontaginfo)
	{
		return commontagMapper.updateTblCommontaginfo(tblCommontaginfo);
	}

	/**
	 * 批量删除部门公共标签
	 *
	 * @param ids 需要删除的部门公共标签ID
	 * @return 结果
	 */
	
	public int deleteTblCommontaginfoByIds(Long[] ids)
	{
		return commontagMapper.deleteTblCommontaginfoByIds(ids);
	}

	/**
	 * 删除部门公共标签信息
	 *
	 * @param id 部门公共标签ID
	 * @return 结果
	 */
	
	public int deleteTblCommontaginfoById(Long id)
	{
		return commontagMapper.deleteTblCommontaginfoById(id);
	}
	
}
